import React, { Component } from 'react';
import UploadImageSample from '@/components/UploadImageSample';
import { Form, Input, Button, Row, Col, message, DatePicker, Upload, Icon, Modal } from 'antd';
import { connect } from 'dva';
const FormItem = Form.Item;
const { RangePicker } = DatePicker;

@Form.create()
@connect(({ loading }) => ({
  loading: loading.effects['authentication/sendAuthentication'],
}))
class Authentication extends Component {
  state = {
    confirmDirty: false,
  };

  handleChange = ({ fileList }) => this.setState({ fileList });

  handleSubmit = e => {
    e.preventDefault();
    const { form, dispatch, changeStatus } = this.props;
    form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        dispatch({
          type: 'authentication/sendAuthentication',
          payload: values,
        })
          .then(msg => {
            changeStatus(true);
          })
          .catch(errMsg => {
            message.error(errMsg);
          });
      }
    });
  };
  normFile = e => {
    console.log('Upload event:', e);
    if (Array.isArray(e)) {
      return e;
    }
    return e && e.fileList;
  };

  render() {
    const {
      form: { getFieldDecorator },
      loading,
    } = this.props;
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
    };
    const formItemLayout2 = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 12 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 },
      },
    };
    const rangeConfig = {
      rules: [{ type: 'array', required: true, message: 'Please select time!' }],
    };
    const uploadButton = (
      <div>
        <Icon type="plus" />
        <div className="ant-upload-text">上传</div>
      </div>
    );
    return (
      <Form onSubmit={this.handleSubmit}>
        <Row>
          <Col span={12}>
            <FormItem label="姓名" {...formItemLayout}>
              {getFieldDecorator('name', {
                rules: [
                  {
                    required: true,
                    message: '请输入姓名',
                  },
                ],
              })(<Input />)}
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={12}>
            <FormItem label="身份证号" {...formItemLayout}>
              {getFieldDecorator('range-picker', rangeConfig)(<Input />)}
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={12}>
            <FormItem label="证件有效期" {...formItemLayout}>
              {getFieldDecorator('range-picker', rangeConfig)(<RangePicker />)}
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={6}>
            <FormItem
              {...formItemLayout2}
              label="上传证件"
              extra={<span style={{ color: '#fecc6c' }}>个人信息页</span>}
            >
              {getFieldDecorator('upload1', {
                valuePropName: 'fileList',
                getValueFromEvent: this.normFile,
                initialValue: [],
              })(
                <Upload
                  action="//jsonplaceholder.typicode.com/posts/"
                  listType="picture-card"
                  onPreview={this.handlePreview}
                  onChange={this.handleChange}
                >
                  {uploadButton}
                </Upload>
              )}
            </FormItem>
          </Col>
          <Col span={4}>
            <UploadImageSample />
          </Col>
          <Col span={3}>
            <FormItem {...formItemLayout2} extra={<span style={{ color: '#fecc6c' }}>国徽页</span>}>
              {getFieldDecorator('upload2', {
                valuePropName: 'fileList',
                getValueFromEvent: this.normFile,
              })(
                <Upload
                  action="//jsonplaceholder.typicode.com/posts/"
                  listType="picture-card"
                  onPreview={this.handlePreview}
                  onChange={this.handleChange}
                >
                  {uploadButton}
                </Upload>
              )}
            </FormItem>
          </Col>
          <Col span={4}>
            <UploadImageSample />
          </Col>
        </Row>
        <Row>
          <Col offset={3} style={{ marginTop: 15, marginBottom: 35 }}>
            支持.png .pdf .jpg格式上传，大小5m以内.
          </Col>
          <Col span={24} style={{ textAlign: 'center' }}>
            <Button type="primary" htmlType="submit" disabled={loading}>
              {loading ? '请稍后' : '下一步'}
            </Button>
          </Col>
        </Row>
      </Form>
    );
  }
}

export default Authentication;
